package com.jt.controller;

import com.jt.pojo.Manager;
import com.jt.service.ManagerService;
import com.jt.utils.CookieUtil;
import com.jt.utils.ObjectMapperUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 管理员：登录  注册   修改  查看  退出
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/manager")
public class ManagerConctroller {
	@Autowired
	private ManagerService managerService;
	@Autowired
	JedisCluster jedis;
	
	/**
	 * RESTful风格：跳转到登录  注册
	 */
	@RequestMapping("/{requestName}")
	public String toLonginOrRegister(@PathVariable String requestName) {
		return requestName;
	}
	
	
	/**
	 * 注册: 实现用户新增
	 * url:/manager/doRegister
	 * 参数：username   password   phone
	 * 返回值：SysResult
	 */
	@RequestMapping("/doRegister")
	@ResponseBody
	public SysResult doRegister(Manager manager) {
		return managerService.saveManager(manager);
	}
	
	/**
	 * 登录验证
	 * url:/manager/doLogin
	 * 参数：username  password  response:添加cookie
	 * 返回值：SysResult
	 */
	@RequestMapping("/doLogin")
	@ResponseBody
	public SysResult login(Manager manager, HttpServletResponse response) {
		SysResult result = managerService.findByUP(manager);
		//用户名和密码存在，添加cookie  缓存
		if(result.getStatus()==200) {  
			String cookieValue = UUID.randomUUID().toString();
			Cookie cookie = new Cookie("MANAGER", cookieValue);
			cookie.setDomain("jt.com");       //设置cookie共享范围, jt.com域共享
			cookie.setPath("/");              //cookie权限
			cookie.setMaxAge(7*24*3600);      //存活时间7天
			response.addCookie(cookie);		 // 添加cookie
			//保存到缓存：存入假的密码
			Manager objManager = (Manager)result.getData();
			objManager.setPassword("你猜吧！");
			String jsonManager = ObjectMapperUtil.toJson(objManager);
			jedis.setex(cookieValue, 7*24*3600, jsonManager);
		}
			return result;
	}
	
	/**
	 * 修改密码：
	 * url:/manager/toUpdatePassword
	  * 参数：password  newPassword  request
	  * 返回值：SysResult  
	 */
	@RequestMapping("/toUpdatePassword")
	@ResponseBody
	public SysResult toUpdatePassword(HttpServletRequest request, String password, String newPassword) {
		//获取用户id
		String cookieValue = CookieUtil.getCookieValue(request, "MANAGER");
		String string = jedis.get(cookieValue);
		Manager objManager = ObjectMapperUtil.toObject(string, Manager.class);
		Long id = objManager.getId();
		
		return managerService.matchAndUpdate(id, password, newPassword);
	}
	
	
	/**
	 * 退出登录
	 * url:/manager/loginOut
	  * 参数：request response
	 * 返回值：SysResult
	 */
	@RequestMapping("/loginOut")
	@ResponseBody
	public SysResult longinOut(HttpServletRequest request, HttpServletResponse response) {
	    //获取cookie值
		String cookieValue = CookieUtil.getCookieValue(request, "MANAGER");
		//判断缓存是否存在:删除缓存和cookie
		if(!StringUtils.isEmpty(cookieValue)) {
			jedis.del(cookieValue);
			CookieUtil.deleteCookie(response, "MANAGER", "jt.com", "/");
		}
		return SysResult.success();
	}
	
}
